//
//  XLGDataLinkWorkShop.m
//  etionCBLogic
//
//  Created by wjxfhxy on 14-10-13.
//  Copyright (c) 2014年 GuangZhouXuanWu. All rights reserved.
//

#import "XLGDataLinkWorkShop.h"

@implementation XLGDataLinkWorkShop

+(void)linkDatas:(NSArray*)datas
{
    for(NSInteger i =0 ;i < datas.count - 1 ;i ++)
    {
        id<XLGDataLinkWorkShopPorpertyDelegate> data1 = datas[i];
        id<XLGDataLinkWorkShopPorpertyDelegate> data2 = datas[i + 1];
        data1.followID = data2.parentID;
    }
}

+(void)data:(id<XLGDataLinkWorkShopPorpertyDelegate>)message linkFollowData:(id<XLGDataLinkWorkShopPorpertyDelegate>)followMessage
{
    message.followID = followMessage.parentID;
}

+(BOOL)checkDataLink:(NSArray*)dataLink
{
    for(NSInteger i =0 ;i < dataLink.count - 1 ;i ++)
    {
        id<XLGDataLinkWorkShopPorpertyDelegate> data1 = dataLink[i];
        id<XLGDataLinkWorkShopPorpertyDelegate> data2 = dataLink[i + 1];
        if([data1.followID isEqualToString:data2.parentID])
            continue;
        else
            return NO;
    }
    return YES;
}

@end

@implementation XLGDataLinkWorkShop (SQLiteHelper)

+(NSInteger)saveDataLinks:(NSArray*)dataLink withLastParentID:(NSString*)lastParentID andSQLiteHelper:(XMSSQLiteHelper *)handler updateTable:(NSString*)tableName setKey:(NSString*)setKey whereKey:(NSString*)whereKey
{
    if(dataLink.count <=0 )
        return NO;
    
    XMSSQLiteHelper *sqlHelper = [[[XMSSQLiteHelper alloc]initWithSQLiteHelper:handler] autorelease_mm];
    NSInteger nResult = [sqlHelper transExce:^NSInteger
    {
        NSInteger nResult = FALSE;
        
        if(lastParentID.length > 0)
        {
            NSString *followID = ((id<XLGDataLinkWorkShopPorpertyDelegate>)dataLink[0]).parentID;
            NSString *sql = [NSString stringWithFormat:@"update %@ set %@ = '%@' where %@ = '%@'",tableName,setKey,followID,whereKey,lastParentID];
            nResult = [sqlHelper immediateRunSQL:[sql UTF8String]];
            if(nResult != YES)
                return nResult;
        }
        
        if(dataLink.count > 1)
        {
            for(NSInteger i = 0; i < dataLink.count - 1 ;i++)
            {
                id<XLGDataLinkWorkShopPorpertyDelegate> message = dataLink[i];
                
                NSString *parentID = message.parentID;
                NSString *followID = message.followID;
                NSString *sql = [NSString stringWithFormat:@"update %@ set %@ = '%@' where %@ = '%@'",tableName,setKey,followID,whereKey,parentID];
                nResult = [sqlHelper immediateRunSQL:[sql UTF8String]];
                if(nResult != YES)
                    return nResult;
            }
        }
        else if(dataLink.count == 1)
        {
            id<XLGDataLinkWorkShopPorpertyDelegate> message = dataLink[0];
            NSString *sql = [NSString stringWithFormat:@"update %@ set %@ = '%@' where %@ = '%@'",tableName,setKey,@"",whereKey,message.parentID];
            nResult = [sqlHelper immediateRunSQL:[sql UTF8String]];
        }
        else
            nResult = YES;
        
        return nResult;
    }];
    return nResult;
}


@end
